layui.config({
    base: '/static/js/lib/',
    version: new Date().getTime()
}).use(['layer', "table", "laytpl", "laydate"], function () {

    $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var laytpl = layui.laytpl;
    var laydate = layui.laydate;
    laydate.render({
        elem: '#beginTime' //指定元素
        , type: 'datetime'
    });

    $('.select').click(function () {
        selectClick($(this));
    });

    $(".inputSelect").click(function () {
        selectClick($(this));
    }).blur(function () {
        selectBlur($(this));
    });

    // 企业下拉框选择
    $('.company_select dd').mousedown(function () {
        select($(this));
    });

    /**
     * 获取标段地址
     */
    function address(companyId) {
        $.post("/selection/address/list", {companyId: companyId}, function (res) {
            if (res.code == "00000") {
                var address = res.data;
                var html = "";
                for (var i = 0; i < address.length; i++) {
                    html += '<dd value="' + address[i].id + '" type="addressTenders" class="" style="">' + address[i].name + '</dd>';
                }
                $('.addressTenders_select').html(html);
                // 下拉框选择
                $('.addressTenders_select dd').mousedown(function () {
                    select($(this));
                });
            } else {
                console.log(res);
            }
        });
    }

    function select(_this) {
        var id = _this.attr("type");
        var value = _this.attr("value");
        $('#' + id).val(value);
        $('input[name="' + id + '"]').val(_this.html());
        _this.addClass("layui-this").siblings().removeClass("layui-this");
        _this.parents(".layui-form-selected").removeClass("layui-form-selected");
        $('.' + id + '_select').css('display', 'none');
        if (id == "company") {
            address(value)
        }

        if (id == "format") {
            phasesList(value)
        }

        if (id == "scheme") {
            schemeList(value)
        }
    }


    function selectClick(_this) {
        var id = _this.attr("type");
        if (null == id || undefined == id || id == "text") {
            id = _this.parent().attr("type")
        }
        $('.' + id + '_select').css('display', 'block');
        _this.parents(".layui-form-select").addClass("layui-form-selected");
    }

    function selectBlur(_this) {
        var id = _this.attr("type");
        if (null == id || undefined == id || id == "text") {
            id = _this.parent().attr("type")
        }
        var parent = _this.parents(".layui-form-selected");
        parent.removeClass("layui-form-selected"); // 上下箭头
        $('.' + id + '_select').css('display', 'none'); // 是否显示下拉框
        var name = $('input[name="' + id + '"]').val().trim();
        var status = false;
        $('.' + id + '_select dd').each(function () {
            if (name == $(this).html().trim()) {
                status = true;
            }
        });
        if (!status) {
            $('#' + id).val("")
        }

        if (id == "company" && $('#' + id).val() != "") {
            address($('#' + id).val())
        }

        if (id == "company" && $('#' + id).val() == "") {
            $('.addressTenders_select').html("");
        }

        if (id == "format" && $('#' + id).val() == "") {
            $('.phases_select').html("");
        }

    }

    phasesList();
    schemeList();

    function schemeList(parentId) {
        var type = "scope";
        if (undefined == parentId) {
            parentId = null;
            type = "scheme";
        }
        $.post("/scheme/list", {parentId: parentId}, function (res) {
            if (res.code == "00000") {
                var scheme = res.data;
                var html = "";
                var li = "";
                for (var i = 0; i < scheme.length; i++) {
                    html += '<dd value="' + scheme[i].id + '" type="' + type + '" class="" style="">' + scheme[i].name + '</dd>';
                    li += '<li class="page_btn">' + scheme[i].name +
                        '<i class="remove" value="' + scheme[i].id + '" type="' + type + '">X</i> </li>'
                }

                if (null == parentId) {
                    $('.scheme_select').html(html);
                    // 下拉框选择
                    $('.scheme_select dd').mousedown(function () {
                        select($(this));
                    });
                } else {
                    $('.scope_select').html(html);
                    $('#scope-container').html(li);
                    $(".remove").click(function () {
                        remove($(this))
                    });
                    // 下拉框选择
                    $('.scope_select dd').mousedown(function () {
                        select($(this));
                    });
                }

            } else {
                console.log(res);
            }
        });
    }

    // 获取业态和施工阶段
    function phasesList(parentId) {
        var type = "phases";
        if (undefined == parentId) {
            parentId = null;
            type = "format";
        }
        $.post("/phases/list", {parentId: parentId}, function (res) {
            if (res.code == "00000") {
                var format = res.data;
                var html = "";
                var li = "";
                for (var i = 0; i < format.length; i++) {
                    html += '<dd value="' + format[i].id + '" type="' + type + '" class="" style="">' + format[i].name + '</dd>';
                    li += '<li class="page_btn">' + format[i].name +
                        '<i class="remove" value="' + format[i].id + '" type="' + type + '">X</i> </li>'
                }

                if (null == parentId) {
                    $('.format_select').html(html);
                    // 下拉框选择
                    $('.format_select dd').mousedown(function () {
                        select($(this));
                    });
                } else {
                    $('.phases_select').html(html);
                    $('#phases-container').html(li);
                    $(".remove").click(function () {
                        remove($(this))
                    });
                    // 下拉框选择
                    $('.phases_select dd').mousedown(function () {
                        select($(this));
                    });
                }

            } else {
                console.log(res);
            }
        });
    }


    $(".save").click(function () {
        var _this = $(this);
        var type = _this.attr("type");
        var name = $('input[name="' + type + '"]').val();
        var parentId = null;
        var url = "";
        var data = null;
        if (type == "phases") {
            url = "/phases/save";
            parentId = $('#format').val();
            data = {
                parentId: parentId,
                name: name
            }
        } else if (type == "format") {
            url = "/phases/save";
            data = {
                name: name
            }
        } else if (type == "scope") {
            url = "/scheme/save";
            parentId = $('#scheme').val();
            data = {
                parentId: parentId,
                name: name
            }
        } else if (type == "scheme") {
            url = "/scheme/save";
            data = {
                name: name
            }
        }

        $.post(url, data, function (res) {
            if (res.code == "00000") {
                layer.msg("保存成功");
                clean(type)
            } else {
                console.log(res);
            }
        });
    });

    function clean(type) {
        if (type == "phases") {
            phasesList($('#format').val());
        } else if (type == "format") {
            $('.phases_select').html("");
            $('#phases-container').html("");

            $('input[name="phases"]').val("");
            $('#phases').val("")
        } else if (type == "scope") {
            schemeList($('#scheme').val())
        } else if (type == "scheme") {
            schemeList();
            $('.scope_select').html("");
            $('#scope-container').html("");
            $('input[name="scope"]').val("");
            $('#scope').val("")
        }

        $('input[name="' + type + '"]').val("");
        $('#' + type).val("")
    }

    $(".remove").click(function () {
        remove($(this))
    });

    // 删除阶段设置或随机范围
    function remove(_this) {
        var id = null;
        var type = _this.attr("type");
        var url = "";

        if (type == "phases") {
            url = "/phases/delete";
            id = _this.attr("value")
        } else if (type == "format") {
            url = "/phases/delete";
            id = $('#format').val();
        } else if (type == "scheme") {
            url = "/scheme/delete";
            id = $('#scheme').val();
        } else if (type == "scope") {
            url = "/scheme/delete";
            id = _this.attr("value")
        }

        $.post(url, {id: id}, function (res) {
            if (res.code == "00000") {
                layer.msg("删除成功");
                clean(type);
            } else {
                console.log(res);
            }
        });
    }

    // 保存设置
    $('.assessmentSave').click(function () {
        var data = {
            companyId: $('#company').val(),
            schemeId: $('#scheme').val(),
            formatId: $('#format').val(),
            addressTendersId: $('#addressTenders').val(),
            assessmentTime: $('#beginTime').val(),
            companyName: $('input[name="company"]').val(),
            addressTendersName: $('input[name="addressTenders"]').val()
        };

        console.log(data)
        if (isEmpty(data.schemeId)) {
            layer.msg("方案不能为空！")
        } else if (isEmpty(data.formatId)) {
            layer.msg("施工阶段业态不能为空！")
        } else if (isEmpty(data.assessmentTime)) {
            layer.msg("评估时间不能为空！")
        } else if (isEmpty(data.companyName)) {
            layer.msg("企业名称不能为空！")
        } else if (isEmpty(data.addressTenders)) {
            layer.msg("标段地址不能为空！")
        }
        $.post('/assessment/save', data, function (res) {
            if (res.code == "00000") {
                console.log(res);
                $('.random').attr("id", res.data);
                layer.msg("保存成功");
            } else {
                console.log(res);
            }
        });
    });

    function isEmpty(type) {
        if (null == type || undefined == type || "" == type.trim()) {
            return false;
        }
    }


    $('.random').click(function () {
        layer.open({
            type: 2,
            area: ['100%', '100%'],
            fix: false,
            maxmin: true,
            shadeClose: true,
            shade: 0.4,
            title: "抽选规则",
            content: "/assessment?assessmentId=" + $(this).attr("id")
        });
    })

});